<template>
  <div>
    <el-form ref="applyForm" :model="formItem" class="form-view" label-width="100px" size="mini">
      <el-form-item label="领用需求" prop="requireSpare">
        {{ formItem.requireSpare }}
      </el-form-item>
      <el-form-item label="服务厅局" prop="officeId">
        {{ formItem.officeName }}
      </el-form-item>
      <el-form-item label="故障工单" prop="malfunction">
        {{ formItem.malfunctionName }}
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { todoPage, donePage } from '@neu/dcam-ui/api/workOrder'
import { listInstitutions } from '@neu/dcam-ui/api/fixedAsset'
import { detail } from '@neu/dcam-ui/api/spareconsume'
const formItem = {
  requireSpare: '',
  officeId: null,
  malfunction: null
}
export default {
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      offices: [],
      malfunctions: [],
      formItem: {
        ...formItem
      },
      rules: {
        requireSpare: [{ required: true, message: '领用需求不能为空', trigger: 'blur' }],
        officeId: [{ required: true, message: '服务厅局不能为空', trigger: 'change' }],
        malfunction: [{ required: true, message: '故障工单不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.fetch()
    const parameter = {
      workOrderType: 'malfunction',
      page: 0,
      size: 10000
    }
    todoPage(parameter).then(response => {
      this.malfunctions = response.data
      donePage(parameter).then(response => {
        this.malfunctions = this.mergeArray(this.malfunctions, response.data)
      })
    })
    listInstitutions().then(response => {
      this.offices = response.data
    })
  },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    mergeArray(arr1, arr2) {
      // eslint-disable-next-line no-array-constructor
      var _arr = new Array()
      for (var i = 0; i < arr1.length; i++) {
        _arr.push(arr1[i])
      }
      for (let i = 0; i < arr2.length; i++) {
        var flag = true
        for (var j = 0; j < arr1.length; j++) {
          if (arr2[i] === arr1[j]) {
            flag = false
            break
          }
        }
        if (flag) {
          _arr.push(arr2[i])
        }
      }
      return _arr
    }
  }
}
</script>
